package com.itheima.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Product;
import com.itheima.pojo.R;
import com.itheima.service.TaoBaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/sku")
public class TaoBaoController {
    @Autowired
    TaoBaoService taoBaoService;

    @PostMapping()
    public R addProduct(@RequestBody Product product){
        log.info("添加商品");
        taoBaoService.add(product);
        return R.success("添加成功！");
    }

    @GetMapping("/{skuId}")
    public R getById(@PathVariable Long skuId){
        log.info("根据id查询");
        Product product=taoBaoService.getById(skuId);
        return R.success(product);
    }

    @PostMapping("/{pageNum}/{pageSize}")

    public R pageInfo(@PathVariable Integer pageNum,
                      @PathVariable Integer pageSize,
                      String skuName,
                      String priceUp,
                      String priceDown,
                      String classId,
                      String brandName){
        log.info("分页查询");
        PageInfo<Product> pageInfo=taoBaoService.pageInfo(pageNum,pageSize,skuName,
                priceUp,priceDown,classId,brandName);
        return R.success(pageInfo);
    }

    @DeleteMapping("/{skuId}")
    public R deleteById(@PathVariable Long skuId){
        log.info("根据id删除数据");
        taoBaoService.deleteById(skuId);
        return R.success("删除成功!");
    }

    @PutMapping
    public R updateProduct(@RequestBody Product product){
        log.info("修改商品数据");
        taoBaoService.update(product);
        return R.success("修改成功!");
    }

}
